package com.usian.search.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.model.behavior.pojos.ApBehaviorEntry;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.search.dtos.UserSearchDto;
import com.usian.model.search.pojos.ApUserSearch;
import com.usian.model.user.pojos.ApUser;
import com.usian.search.feign.BehaviorFeign;
import com.usian.search.mapper.ApUserSearchMapper;
import com.usian.search.service.ApUserSearchService;
import com.usian.utils.threadlocal.AppThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ApUserSearchServiceImpl
        extends ServiceImpl<ApUserSearchMapper, ApUserSearch>implements ApUserSearchService {

    private static final int DEFAULT_PAGE_SIZE=10;

    @Autowired
    private BehaviorFeign behaviorFeign;

    @Override
    @Async("taskExecutor")
    public void insertApUserSearchService(ApUserSearch apUserSearch) {

        LambdaQueryWrapper<ApUserSearch> querymapper =
                Wrappers.<ApUserSearch>lambdaQuery()
                        .eq(ApUserSearch::getEntryId, apUserSearch.getEntryId())
                        .eq(ApUserSearch::getKeyword, apUserSearch.getKeyword());
        ApUserSearch userSearch = getOne(querymapper);
        if (userSearch!=null && userSearch.getStatus() == 1){
            return;
        }else if (userSearch!=null && userSearch.getStatus() == 0){
            userSearch.setStatus(1);
            updateById(userSearch);
            return;
        }
        //执行新建操作
        apUserSearch.setCreatedTime(new Date());
        apUserSearch.setStatus(1);
        save(apUserSearch);


    }

    @Override
    public ResponseResult querySearchList(UserSearchDto dto) {
        //校验参数
        if (dto.getPageSize()>50){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        if (dto.getPageSize()==0){
            dto.setPageSize(DEFAULT_PAGE_SIZE);
        }

        //获取userid
        ApUser user = AppThreadLocalUtils.getUser();
        if (user==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }

        //查询entry_id
        ApBehaviorEntry apBehaviorEntry = behaviorFeign.queryEntryByUserIdOrEquipmentId(user.getId(), null);


        //调用api做查询
        Page<ApUserSearch>page=new Page<>(0,dto.getPageSize());
        LambdaQueryWrapper<ApUserSearch> queryWrapper =
                Wrappers.<ApUserSearch>lambdaQuery()
                        .eq(ApUserSearch::getEntryId, apBehaviorEntry.getId())
                        .eq(ApUserSearch::getStatus, 1).orderByDesc(ApUserSearch::getCreatedTime);
        IPage<ApUserSearch> apUserSearchIPage=page(page,queryWrapper);
        List<ApUserSearch>resultList=apUserSearchIPage.getRecords();
        //返回

        return ResponseResult.okResult(resultList);
    }

    @Override
    public ResponseResult delSearch(Integer id) {
        //参数校验
        if (id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        //执行更新操作
        LambdaUpdateWrapper<ApUserSearch> updateWrapper =
                Wrappers.<ApUserSearch>lambdaUpdate().eq(ApUserSearch::getId, id).set(ApUserSearch::getStatus, 0);
        boolean updateResult = update(updateWrapper);
        if (!updateResult){
            return ResponseResult.errorResult(AppHttpCodeEnum.DELETE_FAIL);
        }

        //响应结果
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
